Public Class ScanQueue_Factory
    Public Function GetLastQueuedRequest( _
                ByVal lDatabaseID As Long, _
                ByVal lTypeID As SQLAutoDocLib.ScanSchedule.Type) As String

        Dim oFactory As New DBScanQueue_Factory
        Dim oQueue As ScanQueue = oFactory.GetLastQueuedRequest(lDatabaseID, lTypeID)

        If Not oQueue Is Nothing _
            AndAlso IsDate(oQueue.ScanComplete) Then

            Return oQueue.ScanComplete

        ElseIf Not oQueue Is Nothing _
            AndAlso IsDate(oQueue.ScanStart) Then

            Return oQueue.ScanStart

        ElseIf Not oQueue Is Nothing Then
            Return oQueue.CreateDate

        Else
            Return ""
        End If
    End Function

    Public Sub QueueDatabaseForScan(ByVal lDatabaseID As Long)
        Dim oSession As New Session
        oSession.DatabaseID = lDatabaseID
        oSession.Save()

        Dim oDatabase As New Database(lDatabaseID)
        oDatabase.Load()

        Dim oQueue As New ScanQueue
        oQueue.ServerID = oDatabase.ServerID
        oQueue.DatabaseID = lDatabaseID
        oQueue.SessionID = oSession.ID
        oQueue.Save()
    End Sub

    Public Sub QueueServerForScan(ByVal lServerID As Long)
        Dim oFactory As New Database_Factory

        For Each oDatabase As Database In oFactory.GetAllDatabasesForServer(lServerID)
            QueueDatabaseForScan(oDatabase.ID)
        Next
    End Sub

    Public Function GetOpenQueue( _
        ByVal lServerID As Long, _
        ByVal lDatabaseID As Long, _
        ByVal lSessionID As Long, _
        ByVal lScanTypeID As ScanSchedule.Type) As ScanQueue

        Dim oFactory As New DBScanQueue_Factory

        Return oFactory.GetOpenQueue(lServerID, lDatabaseID, lSessionID, lScanTypeID)
    End Function

    Public Function GetNextQueuedRequest() As ScanQueue
        Dim oFactory As New DBScanQueue_Factory

        Return oFactory.GetNextQueuedRequest()
    End Function

    Public Function GetNextQueuedRequest(ByVal lDatabaseID As Long, ByVal lScanTypeID As ScanSchedule.Type) As ScanQueue
        Dim oFactory As New DBScanQueue_Factory

        Return oFactory.GetNextQueuedRequest(lDatabaseID, lScanTypeID)
    End Function

    Public Function GetInprocessRequest( _
                    ByVal lDatabaseID As Long, _
                    ByVal lScanTypeID As ScanSchedule.Type) As ScanQueue

        Dim oFactory As New DBScanQueue_Factory

        Return oFactory.GetInprocessRequest(lDatabaseID, lScanTypeID)
    End Function

    Public Sub RemoveCompletedRequests( _
                    ByVal iMinAgeInDays As Integer)

        Dim oFactory As New DBScanQueue_Factory

        oFactory.RemoveCompletedRequests(iMinAgeInDays)
    End Sub

    Public Sub RemoveTimedoutRequests( _
                    ByVal iMinAgeInMinutes As Integer)

        Dim oFactory As New DBScanQueue_Factory

        'oFactory.RemoveTimedoutRequests(iMinAgeInMinutes)
    End Sub

    Public Function GetCountOfIncompleteRequestsForSession(ByVal lSessionID As Long) As Integer
        Dim oFactory As New DBScanQueue_Factory

        Return oFactory.GetCountOfIncompleteRequestsForSession(lSessionID)
    End Function

    Public Function GetCountOfIncompleteRequestsForDatabase(ByVal lDatabaseID As Long) As Integer
        Dim oFactory As New DBScanQueue_Factory

        Return oFactory.GetCountOfIncompleteRequestsForDatabase(lDatabaseID)
    End Function

    Public Function GetCountOfIncompleteRequestsForServer(ByVal lServerID As Long) As Integer
        Dim oFactory As New DBScanQueue_Factory

        Return oFactory.GetCountOfIncompleteRequestsForServer(lServerID)
    End Function
End Class
